﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Resources;
using PMPUtils.DB;
using CrystalDecisions.Web;

namespace PMPUtils.Report
{
    public class TimeOperator
    {

        public static void RefreshTimeLogging(CrystalReportSource CrystalReportSource1, String startDate, String endDate)
        {
            String resourceList = ResourceOperator.ReadResouceList("'NF1', 'NF2', 'OPTM'");

            String strQueryTimeLoggin = " select " +
                " worklog.author Assignee, " +
                " cast(sum(cast(worklog.timeworked/3600 as decimal(18,2))) as decimal(18,2)) 'Total Working Hours' " +
                " from worklog " +
                " where author in (" + resourceList + ") ";

            strQueryTimeLoggin += "   and datepart(year, worklog.startdate) = datepart(year, '" + startDate + "')  ";
            strQueryTimeLoggin += "   and datepart(wk, worklog.startdate) = datepart(wk, '" + endDate + "') ";

            strQueryTimeLoggin += "   and datepart(dw, worklog.startdate) >= 0 and datepart(dw, worklog.startdate) <= 6 " +
                " group by worklog.author " +
                " order by cast(sum(cast(worklog.timeworked/3600 as decimal(18,2))) as decimal(18,2)) desc";

            DBOperator jiraOperator = new DBOperator(DBConfigurations.JIRA_SQL_DB_CONNECTION);
            DataTable dtTimeLoggin = jiraOperator.Read(strQueryTimeLoggin);
            CrystalReportSource1.ReportDocument.SetDataSource(dtTimeLoggin);

            CrystalReportSource1.ReportDocument.SetDatabaseLogon(DBConfigurations.JIRA_SA, DBConfigurations.JIRA_PASSWORD);
            CrystalReportSource1.ReportDocument.Refresh();
        }
    }
}
